import makebase
import nltk
"""
Run this only once, if you need to restart and do the setup again for some reason, delete the tribase DB.
"""
class Setup:
    
        
    def __init__(self):
        self.tribase=makebase.tribase();
        self.trigrammer();
        self.tagsetter();
    def trigrammer(self):
        trigs=dict();
        s= nltk.data.load('corpora/gutenberg/blake-poems.txt', format='raw')
        s=s.lower();
        #Take out useless charachters, these can be added back in human evaluation!
        s=s.replace(':',' ');
        s=s.replace(',',' ');
        s=s.replace('.',' ');
        s=s.replace(';',' ');
        s=s.replace('"',' ');
        s=s.replace("'",' ');
        s=s.replace("!",' ');
        s=s.replace("(",' ');
        s=s.replace(")",' ');
        s=s.replace("?",' ');
        s=s.replace("-",' ');
        s=s.replace("&",' ');
        a=nltk.tokenize.word_tokenize(s);
        a=nltk.pos_tag(a);
        tr=list((word, tag) for word, tag in a);
        for i in range(len(tr))[0:len(tr)-2]:
            k=tr[i],tr[i+1],tr[i+2];
            trigs[(k)]=1;
            i=i+1;
        #Sorting is not necessary here, but hey why not!
        trigs=sorted(trigs.items(), key=lambda x: x[1], reverse=True);
        self.tribase.makeTrigBase(trigs);
        return; 
    def tagsetter(self):
        tagset=dict();
        s=nltk.data.load('corpora/gutenberg/shakespeare-macbeth.txt', format='raw')
        s=s.lower();
        #Take out useless charachters, these can be added back in human evaluation!
        s=s.replace(':',' ');
        s=s.replace(',',' ');
        s=s.replace('.',' ');
        s=s.replace(';',' ');
        s=s.replace('"',' ');
        s=s.replace("'",'');
        s=s.replace("!",' ');
        s=s.replace("(",' ');
        s=s.replace(")",' ');
        s=s.replace("?",' ');
        s=s.replace("-",' ');
        s=s.replace("&",' ');
        a=nltk.tokenize.word_tokenize(s);
        a=nltk.pos_tag(a);
        tags=list(tag for word, tag in a);
        bads='IN', 'CC','TO', 'DT';
        for i in range(len(tags))[0:len(tags)-4]:
            k=tags[i],tags[i+1],tags[i+2],tags[i+3],tags[i+4];
            if not (k[4] in bads):
                    tagset[(k)]=1;
        #Sorting is not necessary here, but hey why not!
        tagset=sorted(tagset.items(), key=lambda x: x[1], reverse=True);
        self.tribase.makeTagBase(tagset);
        return;
a=Setup();

    